home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 23 / AACD 23.iso / AACD / Programming / tek / msg / sendmsg.c < prev   
C/C++ Source or Header  |  2001-05-12  |  1KB  |  60 lines

  1.  
  2. #include "tek/msg.h"
  3. #include "tek/kn/exec.h"
  4. #include "tek/debug.h"
  5.  
  6. /* 
  7. **    TEKlib
  8. **    (C) 2001 TEK neoscientists
  9. **    all rights reserved.
  10. **
  11. **    TAPTR TSendMsg(TAPTR task, TPORT *msgport, TAPTR msg)
  12. **
  13. **    send message, blocking. returns either the ack'd or replied
  14. **    message, or TNULL for failure.
  15. **
  16. */
  17.  
  18. TAPTR TSendMsg(TAPTR task, TPORT *msgport, TAPTR mem)
  19. {
  20.     TAPTR reply = TNULL;
  21.  
  22.     if (task && msgport && mem)
  23.     {
  24.         TMSG *msg = ((TMSG *) mem) - 1;
  25.  
  26.         msg->replyport = TTaskSyncPort(task);                /* replyport is task's syncport */
  27.         msg->sender = TNULL;                                /* sender is local address space */
  28.  
  29.         msg->status = TMSG_STATUS_SENT | TMSG_STATUS_PENDING;
  30.  
  31.         kn_lock(&msgport->lock);
  32.         TAddTail(&msgport->msglist, (TNODE *) msg);
  33.         TSignal(msgport->sigtask, msgport->signal);
  34.         kn_unlock(&msgport->lock);
  35.  
  36.         for (;;)
  37.         {
  38.             TWait(task, TTaskSyncPort(task)->signal);
  39.             reply = TGetMsg(TTaskSyncPort(task));
  40.             if (reply)
  41.             {
  42.                 break;
  43.             }
  44.             tdbprintf(10,"TEKLIB TSendMsg(): WARNING: got signal on syncport with no message\n");
  45.         }
  46.  
  47.         if (reply != mem)
  48.         {
  49.             tdbprintf(40,"TEKLIB TSendMsg(): ALERT: message returned was not sent\n");
  50.         }
  51.  
  52.         if (TGetMsgStatus(reply) == TMSG_STATUS_FAILED)
  53.         {
  54.             reply = TNULL;            /* indicate failure */
  55.         }
  56.     }
  57.  
  58.     return reply;
  59. }
  60.